// "Import" the StateManager so we don't have to keep typing
// "EXANIMO.managers.StateManager".
window.StateManager = EXANIMO.managers.StateManager;

// Call the old navigation function when the user pushes the back
// button, forward button, or visits a deep-link.
StateManager.stateRevisitedHandler = function(e){
   showPage(e.id);
};

var pageIdSeparator = ".-";
function getPageId(file, trip, point) {
	var id = file + pageIdSeparator;
	if (trip != undefined) id += trip;
	id += pageIdSeparator;
	if (point != undefined) id += point;
	return id;
}
function getPageTitle(file, trip, point) {
	var id = file;
	if (file != "" && trip != undefined) id += " > ";
	if (trip != undefined) id += trip;
	if (point != undefined && point != "") id += " > " + point;
	return id;
}
function showPage(id) {
	var parts = id.split(pageIdSeparator);
	var file = unescape(parts[0]);
	var trip = unescape(parts[1]);
	var point = unescape(parts[2]);
	var isSameFile = currentFile != undefined && file != undefined && currentFile == file;
	var isSameTrip = currentTrip != undefined && trip != undefined && trip != "" && currentTrip == trip;
	var isSamePoint = currentAPoint != undefined && point != undefined && point != "" && currentAPoint == point;
	//alert("stateusage showPage "+ file +", "+trip+", "+point+", sf="+isSameFile+", st="+isSameTrip+", sp="+isSamePoint);

	if(point == undefined || point == "") {
		if(trip == undefined || trip == "") {
			if(file == undefined) {
				// show default map
				callTrips("", "", "");
			} else {
				if (!isSameFile) {
					callTrips(file, "", "");
				}
			}
		} else {
			if(file == undefined) {
				// show default map
				callTrips("", "", "");
			} else {
				if (!isSameFile) {
					callTrips(file, trip, "");
				} else if (!isSameTrip) {
					callTrip(file, trip);
				}
			}
		}
	} else {
		if(trip == undefined || trip == "") {
			if(file == undefined) {
				// show default map
				callTrips("", "", "");
			} else {
				if (!isSameFile) {
					callTrips(file, "", "");
				}
			}
		} else {
			if(file == undefined) {
				// show default map
				callTrips("", "", "");
			} else {
				if (!isSameFile) {
					callTrips(file, trip, point);
				} else if (!isSameTrip) {
					callTrip(file, trip, point);
				} else if (!isSamePoint) {
					selectPointName(point);
				}
			}
		}
	}
}

